/**
 * 
 */
package net.looker.config;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.logging.Logger;

import org.jdom.JDOMException;

import net.looker.data.Category;

/**
 * 配置管理器
 * 管理所有的配置，并且全局共享一个实例
 * @author solosky
 *
 */
public class ConfigManager
{
	
	/**
	 * 单例对象	
	 */
	private static ConfigManager configManager = new ConfigManager();
	/**
	 * Looker的配置
	 */
	private LookerConfig lookerConfig;
	
	/**
	 * 多个站点的配置
	 */
	private ArrayList<SiteConfig> siteConfigList;
	
	/**
	 * 栏目hash=>站点配置key
	 */
	private HashMap <String,Integer> categorySiteHash;
	
	/**
	 * 返回实例
	 */
	public static ConfigManager getInstance()
	{
		return configManager;
	}
	
	/**
	 *构造函数 
	 */
	public ConfigManager()
	{
		 siteConfigList   = new ArrayList<SiteConfig>();
		 categorySiteHash = new HashMap <String,Integer>();
	}
	/**
	 * 加载looker全局配置
	 * @param  lookerConfigFile
	 * @throws IOException 
	 * @throws JDOMException 
	 * @throws IOException 
	 * @throws FileNotFoundException 
	 */
	public void	loadLookerConfig(String lookerConfigFile) throws JDOMException, IOException
	{
			//Looker配置
			lookerConfig = new LookerConfig(lookerConfigFile);
			
	}
	
	public void loadSiteConfigs()
	{
			//遍历站点配置XML文件，建立每个站点的配置对象
			SiteConfig siteConfig = null;
			Logger logger = Logger.getLogger("looker");
			
			File dir = new File(lookerConfig.get("looker.siteConfigDir"));
			if(!dir.exists()){
				logger.warning("站点配置不存在");
				return ;
			}
			
			File[] files = dir.listFiles();
			
			for (int i=0; i<files.length; i++)
			{
				if(files[i].isFile())
				{
					//建立站点配置并加入站点配置列表
					try {
						siteConfig = new SiteConfig(files[i]);
						siteConfig.buildCategoryList();
						siteConfigList.add(siteConfig);
					
						//建立栏目=>站点配置的哈希表，便于查找配置
						Iterator<Category> it = siteConfig.getCategoryList().iterator();
						while(it.hasNext())
							categorySiteHash.put(it.next().getCategoryHash(), siteConfigList.indexOf(siteConfig));
						
						logger.info("成功加载站点配置:"+siteConfig.get("site.title")+"|"+siteConfig.get("site.url"));
					} catch (FileNotFoundException e) {
					} catch (IOException e) {
					} catch (JDOMException e) {
						logger.warning("配置文件解析错误："+files[i].toString());
					}
				}
			}
	}
	
	/**
	 * 返回配置文件列表
	 */
	public ArrayList <SiteConfig> getSiteConfigList()
	{
		return siteConfigList;
	}
	
	/**
	 * 返回looker的配置文件
	 * @return
	 */
	
	public Config getLookerConfig()
	{
		return lookerConfig;
	}
	
	/**
	 * 根据站点的栏目哈希查询站点配置
	 * @param categoryHash
	 * @return
	 */
	public SiteConfig getSiteConfig(String categoryHash)
	{
		Integer key = 0;
		SiteConfig  siteConfig = null;
		//根据栏目哈希查询站点配置的下标
		if ( (key=categorySiteHash.get(categoryHash))==null)
			return null;
		//根据站点下标查询站点配置
		if ( (siteConfig=siteConfigList.get(key))==null)
			return null;
		else
			return siteConfig;
	}
	
	/**
	 * 同步站点配置
	 */
	public void SynchConfig()
	{
		 ConfigSynchronizer configSynch = new  ConfigSynchronizer();
		 configSynch.queryConfig();
		 Iterator <SiteConfig> it = siteConfigList.iterator();
		 while (it.hasNext())		 
			 configSynch.synchronize(it.next());
	}
	
	/**
	 * 返回栏目总数
	 * @return
	 */
	public int getCategoryCount()
	{
		return this.categorySiteHash.size();
	}
	
	/**
	 * 返回站点总数
	 * @return
	 */
	public int getSiteCount()
	{
		return this.siteConfigList.size();
	}
}
